export class LevelManager {
    constructor() {
        this.currentLevel = 1;
        this.currentTheme = 'forest';
        this.baseScoreThreshold = 2000; // 基础升级分数
        this.accumulatedScore = 0;     // 累计实际分数
        this.displayScore = 0;         // 显示分数
        this.baseSpeedMultiplier = 1;  // 基础速度倍率
        
        // 定义不同主题的样式
        this.themes = {
            forest: {
                name: '森林',
                backgroundColor: '#87CEEB',
                obstacleColor: '#2E8B57',
                groundColor: '#228B22',
                playerColor: '#8B4513',
                bgMusic: 'assets/audio/forest_bg.wav',
                obstacleType: 0  // 尖刺型障碍物
            },
            desert: {
                name: '沙漠',
                backgroundColor: '#FFE4B5',
                obstacleColor: '#DEB887',
                groundColor: '#DAA520',
                playerColor: '#8B7355',
                bgMusic: 'assets/audio/desert_bg.wav',
                obstacleType: 1  // 旋转立方体
            },
            space: {
                name: '太空',
                backgroundColor: '#000033',
                obstacleColor: '#4169E1',
                groundColor: '#483D8B',
                playerColor: '#B8860B',
                bgMusic: 'assets/audio/space_bg.wav',
                obstacleType: 2  // 菱形水晶
            }
        };
    }

    // 更新分数并检查是否需要升级
    updateScore(increment) {
        // 获取当前等级的分数倍率
        const config = this.getLevelConfig();
        const actualIncrement = increment * config.scoreMultiplier;
        
        // 更新累计分数和显示分数
        this.accumulatedScore += increment;        // 实际分数增加原始值
        this.displayScore += actualIncrement;      // 显示分数增加倍率后的值
        
        // 计算下一级所需的实际分数
        const nextLevelThreshold = this.baseScoreThreshold * this.currentLevel;
        
        // 检查是否达到升级条件
        if (this.accumulatedScore >= nextLevelThreshold) {
            this.currentLevel++;
            // 每升一级，基础速度倍率翻倍
            this.baseSpeedMultiplier *= 1.5;
            this.updateTheme();
            return true; // 表示关卡已更新
        }
        return false;
    }

    // 获取升级进度（0-1之间的数值）
    getLevelProgress() {
        const currentLevelThreshold = this.baseScoreThreshold * (this.currentLevel - 1);
        const nextLevelThreshold = this.baseScoreThreshold * this.currentLevel;
        const progress = (this.accumulatedScore - currentLevelThreshold) / (nextLevelThreshold - currentLevelThreshold);
        return Math.min(Math.max(progress, 0), 1);
    }

    // 获取下一级所需分数
    getNextLevelScore() {
        return this.baseScoreThreshold * this.currentLevel;
    }

    // 获取当前显示分数
    getDisplayScore() {
        return Math.floor(this.displayScore);
    }

    // 切换主题
    updateTheme() {
        const themeKeys = Object.keys(this.themes);
        const themeIndex = (this.currentLevel - 1) % themeKeys.length;
        this.currentTheme = themeKeys[themeIndex];
    }

    // 获取当前主题的样式
    getCurrentTheme() {
        const themeKeys = Object.keys(this.themes);
        const themeIndex = (this.currentLevel - 1) % themeKeys.length;
        this.currentTheme = themeKeys[themeIndex];
        return this.themes[this.currentTheme];
    }

    // 获取当前关卡的游戏参数
    getLevelConfig() {
        const theme = this.getCurrentTheme();
        // 调整速度增长曲线
        const speedMultiplier = this.baseSpeedMultiplier; // 使用基础速度倍率
        const frequencyIncrease = Math.min(0.05 * (this.currentLevel - 1), 0.5);
        const scoreMultiplier = 1 + (this.currentLevel - 1) * 0.1;

        return {
            obstacleSpeed: speedMultiplier,
            obstacleFrequency: 1 + frequencyIncrease,
            scoreMultiplier: scoreMultiplier,
            obstacleType: theme.obstacleType
        };
    }

    // 重置关卡系统
    reset() {
        this.currentLevel = 1;
        this.currentTheme = 'forest';
        this.accumulatedScore = 0;
        this.displayScore = 0;
        this.baseSpeedMultiplier = 1;  // 重置基础速度倍率
        return this.getCurrentTheme();
    }
} 